home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / buffr2.zip / BND_MAX.PAS < prev    next >
Pascal/Delphi Source File  |  1993-01-04  |  8KB  |  249 lines

  1. Unit BND_Max;  {Primitive N-Dimensional BufferedArrays}
  2. {$R-,O+,S-}
  3.  
  4. INTERFACE
  5. Uses BuffAray;
  6.  
  7. Type
  8.   DimensionPtr   = ^DimensionArray;
  9.   DimensionArray = Array[0..0] of LongInt;
  10.  
  11.   BN_Max = Object (BufferedArray)
  12.  
  13.                Dim  : Byte;         {PRIVATE}
  14.                Len  : DimensionPtr; {PRIVATE}
  15.  
  16.                Add1 : DimensionPtr; {Provides two arrays of the correct}
  17.                Add2 : DimensionPtr; {size for passing the indices of the}
  18.                                     {N-dimensional array.  this is so all}
  19.                                     {memory needed for the N-D array can}
  20.                                     {be allocated at creation, rather}
  21.                                     {than being done from within applications}
  22.                                     {For correct usage, see any of the}
  23.                                     {BDxARRAYS Units.}
  24.  
  25.                Procedure Init (Dimension : Byte;
  26.                          ElementsPerDimension : DimensionPtr;
  27.                          ElementSize : Word; MaxBuffSize : LongInt;
  28.                          FileName : String);
  29.  
  30.  
  31.                Procedure Load (FileName : String; ElementSize : Word;
  32.                                MaxBuffSize : LongInt; Dimension : Byte;
  33.                                ElementsPerDimension : DimensionPtr);
  34.  
  35.                Procedure Accept (Var El; Address : DimensionPtr;
  36.                                  Dimension : Byte; Size : Word);
  37.                                        {Assign the Addressed El}
  38.  
  39.                Procedure Retrieve (Var El; Address : DimensionPtr;
  40.                                    Dimension : Byte; Size : Word);
  41.                                          {Get the Addressed El}
  42.  
  43.                Procedure Copy (From : BN_Max);
  44.                               {Target *MUST* already be initialized}
  45.                               {to the EXACT same parameters as From}
  46.                               {this will save checking for sufficient}
  47.                               {available Memory!}
  48.  
  49.  
  50.                Procedure Swap (I,J : DimensionPtr; Dimension : Byte);
  51.                               {Swap the Ith and Jth Elements}
  52.  
  53.                Function MaxIndex (Index : Byte) : LongInt;
  54.                                  {Returns the Max legal Index for the}
  55.                                  {Indexth Dimension}
  56.                Procedure Destroy;
  57.  
  58. (* no redefinition needed
  59.  
  60.                Procedure Store;
  61.  
  62.                Function MaxSize : LongInt;   {Report Number of Array Elements}
  63.                Function ElemSize : Word;  {Report Element Size}
  64. *)
  65.           End; {BN_Max}
  66.  
  67. IMPLEMENTATION
  68.  
  69. Procedure Error (Num : Byte);
  70. Begin
  71.   WriteLn;
  72.   Write ('N-DimensionBufferedArray ERROR: ');
  73.   Case Num of
  74.             0 : WriteLn ('Attempted ACCEPT with wrong Number of Dimensions');
  75.             1 : WriteLn ('Attempted RETRIEVE with wrong Number of Dimensions');
  76.             2 : WriteLn ('Attempted COPY with wrong Number of Dimensions');
  77.             3 : WriteLn ('Attempted SWAP with wrong Number of Dimensions');
  78.             4 : WriteLn ('Attempted INIT with ElementSize <= Zero');
  79.             5 : WriteLn ('Attempted INIT with Dimension <= Zero');
  80.           End;
  81.   WriteLn ('**** PROGRAM TERMINATED ****');
  82.   WriteLn;
  83.   Write ('Press <Return> to Continue.... ');
  84.   ReadLn;
  85.   HALT (0)
  86. End;
  87.  
  88. Procedure BN_Max.Init;
  89. Var
  90.   ActualSize : LongInt;
  91.   I          : Byte;
  92.   TempAvail  : LongInt;
  93.   MaxElems   : LongInt;
  94.   TempIndex  : LongInt;
  95. Begin
  96.   If ElementSize <= 0 Then Error (4);
  97.   If Dimension <= 0 Then Error (5);
  98.   Dim := Dimension;
  99.   GetMem (Len,Dimension*SizeOf(LongInt));
  100.   GetMem (Add1,Dim*SizeOf(LongInt));
  101.   GetMem (Add2,Dim*SizeOf(LongInt));
  102.   TempAvail := MemAvail;
  103.   TempAvail := TempAvail - (TempAvail Mod ElementSize);
  104.   TempAvail := TempAvail Div ElementSize; {Max Possible elements}
  105.   MaxElems := 1;
  106.   TempIndex := 0;
  107.   While MaxElems < TempAvail do
  108.     Begin
  109.       TempIndex := TempIndex + 1;
  110.       MaxElems := 1;
  111.       For I := 1 to Dimension do
  112.         MaxElems := MaxElems * TempIndex
  113.     End;
  114.   If MaxElems > TempAvail Then TempIndex := TempIndex - 1; {Max Possible}
  115.                                                           {Dimensional Length}
  116.   For I := 0 to Dimension-1 do
  117.     Begin
  118.       Len^[I] := ElementsPerDimension^[I];
  119.       If Len^[I] <= 0 Then
  120.         Len^[I] := TempIndex
  121.     End;
  122.   ActualSize := Len^[0];
  123.   For I := 1 to Dimension-1 do
  124.     ActualSize := ActualSize * Len^[I];
  125.   BufferedArray.Create;
  126.   BufferedArray.Init (ActualSize,ElementSize,MaxBuffSize,FileName)
  127. End;
  128.  
  129.  
  130. Procedure BN_Max.Load (FileName : String; ElementSize : Word;
  131.                 MaxBuffSize : LongInt; Dimension : Byte;
  132.                 ElementsPerDimension : DimensionPtr);
  133. Var
  134.   ActualSize : LongInt;
  135.   I          : Byte;
  136.   TempAvail  : LongInt;
  137.   MaxElems   : LongInt;
  138.   TempIndex  : LongInt;
  139. Begin
  140.   If ElementSize <= 0 Then Error (4);
  141.   If Dimension <= 0 Then Error (5);
  142.   Dim := Dimension;
  143.   GetMem (Len,Dimension*SizeOf(LongInt));
  144.   GetMem (Add1,Dim*SizeOf(LongInt));
  145.   GetMem (Add2,Dim*SizeOf(LongInt));
  146.   TempAvail := MemAvail;
  147.   TempAvail := TempAvail - (TempAvail Mod ElementSize);
  148.   TempAvail := TempAvail Div ElementSize; {Max Possible elements}
  149.   MaxElems := 1;
  150.   TempIndex := 0;
  151.   While MaxElems < TempAvail do
  152.     Begin
  153.       TempIndex := TempIndex + 1;
  154.       MaxElems := 1;
  155.       For I := 1 to Dimension do
  156.         MaxElems := MaxElems * TempIndex
  157.     End;
  158.   If MaxElems > TempAvail Then TempIndex := TempIndex - 1; {Max Possible}
  159.                                                           {Dimensional Length}
  160.   For I := 0 to Dimension-1 do
  161.     Begin
  162.       Len^[I] := ElementsPerDimension^[I];
  163.       If Len^[I] <= 0 Then
  164.         Len^[I] := TempIndex
  165.     End;
  166.   ActualSize := Len^[0];
  167.   For I := 1 to Dimension-1 do
  168.     ActualSize := ActualSize * Len^[I];
  169.   BufferedArray.Create;
  170.   BufferedArray.Load (FileName,ElementSize,MaxBuffSize)
  171. End;
  172.  
  173. Function BN_Max.MaxIndex (Index : Byte) : LongInt;
  174. Begin
  175.   If Index-1 > Dim Then MaxIndex := 0
  176.   Else
  177.     MaxIndex := Len^[Index-1] - 1
  178. End;
  179.  
  180. Function Map (VirtualAddress : DimensionPtr; Dimension : Byte;
  181.               Len : DimensionPtr) : LongInt;
  182. Var
  183.   I       : Byte;
  184.   Address : LongInt;
  185.   Radix   : LongInt;
  186. Begin
  187.   Address := 0;
  188.   Radix := 1;
  189.   For I := 0 to Dimension-1 do
  190.     Begin
  191.       Address := Address + (VirtualAddress^[I] * Radix);
  192.       If I < Dimension-1 Then Radix := Radix * Len^[I]
  193.     End;
  194.   Map := Address
  195. End;
  196.  
  197. Procedure BN_Max.Accept (Var El; Address : DimensionPtr;
  198.                                  Dimension : Byte; Size : Word);
  199.                                        {Assign the Addressed El}
  200. Var
  201.   ActualAddress : LongInt;
  202. Begin
  203.   If Dimension <> Dim Then Error (0);
  204.   ActualAddress := Map(Address,Dimension,Len);
  205.   BufferedArray.Accept (El,ActualAddress,Size)
  206. End;
  207.  
  208. Procedure BN_Max.Retrieve (Var El; Address : DimensionPtr;
  209.                                    Dimension : Byte; Size : Word);
  210.                                          {Get the Addressed El}
  211. Var
  212.   ActualAddress : LongInt;
  213. Begin
  214.   If Dimension <> Dim Then Error (1);
  215.   ActualAddress := Map(Address,Dimension,Len);
  216.   BufferedArray.Retrieve (El,ActualAddress,Size)
  217. End;
  218.  
  219. Procedure BN_Max.Copy (From : BN_Max);
  220.                               {Target *MUST* already be initialized}
  221.                               {to the EXACT same parameters as From}
  222.                               {this will save checking for sufficient}
  223.                               {available Memory!}
  224. Begin
  225.   If Dim <> From.Dim Then Error (2);
  226.   BufferedArray.Copy (From)
  227. End;
  228.  
  229. Procedure BN_Max.Swap (I,J : DimensionPtr; Dimension : Byte);
  230.                               {Swap the Ith and Jth Elements}
  231. Var
  232.   Actual_I,Actual_J : LongInt;
  233. Begin
  234.   If Dimension <> Dim then Error (3);
  235.   Actual_I := Map(I,Dimension,Len);
  236.   Actual_J := Map(I,Dimension,Len);
  237.   BufferedArray.Swap (Actual_I,Actual_J)
  238. End;
  239.  
  240. Procedure BN_Max.Destroy;
  241. Begin
  242.   FreeMem (Len,Dim*SizeOf(LongInt));
  243.   FreeMem (Add1,Dim*SizeOf(LongInt));
  244.   FreeMem (Add2,Dim*SizeOf(LongInt));
  245.   BufferedArray.Destroy
  246. End;
  247.  
  248. BEGIN
  249. END.